class EntertainmentController < ApplicationController
  layout 'entertainment' 
  include EntertainmentHelper
  
  def index
    #This is for not letting anyone go to entertainment without login in!
    redirect_to :action => 'index', :controller=>'core'
  end
    
  def show
    if(session[:user_id] == nil)
      return redirect_to :action => 'index', :controller=>'core'
    end
    @user = User.find(session[:user_id])
    #check EntertainmentHelper for the following 2 functions
    @current_user = User.find(session[:user_id])
    @entertainments = query_entertainment_events(Integer(params[:how_many_days]))
   
    if(params[:is_it_ajax] == "1")
      @entertainment = Announcement.new
      render :partial => "entertainment/templates/new"
    else
      respond_to do |format|
        format.html # show.html.erb
        format.xml  { render :xml => @entertainment }
      end
    end
  end
  
  def destroy
    entertainment = Announcement.find(params[:entertainment])
    entertainment.destroy
    @entertainments = query_entertainment_events(Integer(params[:how_many_days]))
    render :partial => "entertainment/templates/show_all"
  end
  
    # GET /users/1/edit
  def edit
    @user = User.find(session[:user_id])
    @entertainment = Announcement.find(params[:id])
    @old_entertainment = @entertainment
      if(params[:is_it_ajax] == "1")
      @entertainment = Announcement.new
    else
      respond_to do |format|
        format.html # show.html.erb
        format.xml  { render :xml => @entertainment }
      end
    end
  end
  
    # PUT /users/1
  # PUT /users/1.xml
  def update
    @user = User.find(session[:user_id])
    @entertainment = Announcement.find(params[:old_entertainment])
    respond_to do |format|
      if @entertainment.update_attributes(params[:entertainment])
        flash[:notice] = 'Entertainment was successfully updated.'
        #Here we redirect to main with the user so we dont go out of the session
        format.html { redirect_to :controller => "entertainment", :action => "show", :how_many_days => 7}
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @entertainment.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  def new
    @announcement = Announcement.new
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @entertainment }
    end
  end
  
  
  def create
    @entertainment = Announcement.new(params[:entertainment])
    user = User.find(session[:user_id])
    @entertainment.mail = user.email
    @entertainment.announcement_type = $announcement_type

    respond_to do |format|
      if @entertainment.save
        flash[:notice] =  'Entertainment event succesfully loaded '+user.name
        format.html { redirect_to :controller => "entertainment", :action => "show", :how_many_days => 7}

      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @entertainment.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  def more_info 
     @user = User.find(session[:user_id])
    @entertainment = Announcement.find(params[:ent])
    render :partial => "entertainment/templates/more_info" 
  end
  
end
